package com.lehecai.core.lottery.fetcher.dc.impl.worker;

import com.lehecai.core.lottery.DcSaleStatus;
import com.lehecai.core.lottery.fetcher.dc.AbstractDcScheduleFetchWorker;
import com.lehecai.core.lottery.fetcher.dc.DcScheduleItem;
import com.lehecai.core.util.CharsetConstant;
import com.lehecai.core.util.CoreFetcherUtils;
import com.lehecai.core.util.CoreJSONUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class CommonDcScheduleFetchWorkerPengineAPI extends AbstractDcScheduleFetchWorker{
	private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
	
	private static final String PROCESS_CODE = "10003"; 
	
	private static final String URL_PENGINE_API = "http://phase.lehecai.com:16041/LsshServlet";
	//private static final String URL_PENGINE_API = "http://172.16.3.83:6041/LsshServlet";//qa测试用
	
	@Override
	public List<DcScheduleItem> fetchDcSchedule(String phase) {

		if (phase == null || phase.equals("")) {
			logger.error("不支持未指定彩期的抓取");
			return null;
		}
		
		Map<String, String> params = new HashMap<String, String>();
		params.put("ProcessCode", PROCESS_CODE);
		params.put("phase", phase);
		
		String html = CoreFetcherUtils.URLGet(URL_PENGINE_API, params, CharsetConstant.CHARSET_UTF8);
		
		JSONArray jsonArray = null;
		try {
			jsonArray = JSONArray.fromObject(html);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		if (jsonArray == null) {
			logger.error("未抓取到开奖结果");
			return null;
		}
		
		List<DcScheduleItem> items = new ArrayList<DcScheduleItem>();
		for (Iterator iterator = jsonArray.iterator(); iterator.hasNext();) {
			JSONObject object = (JSONObject) iterator.next();
			if (object == null) {
				continue;
			}
			DcScheduleItem item = new DcScheduleItem();
			item.setLeague(CoreJSONUtils.getString(object, "league"));
			item.setMatchIndex(CoreJSONUtils.getInt(object, "matchIndex"));
			item.setMatchTime(CoreJSONUtils.getDate(object, "matchTime", "yyyyMMddHHmmss"));
			item.setPhase(CoreJSONUtils.getString(object, "phase"));
			item.setHomeTeam(CoreJSONUtils.getString(object, "homeTeam"));
			item.setAwayTeam(CoreJSONUtils.getString(object, "awayTeam"));
			item.setHandicap(CoreJSONUtils.getString(object, "handicap"));
			Integer sfpValue = CoreJSONUtils.getInt(object, "sfpStatus");
			if (sfpValue == null || sfpValue == 0) {
				sfpValue = DcSaleStatus.SALE_UNOPEN.getValue();
			}
			Integer sxdsValue = CoreJSONUtils.getInt(object, "sxdsStatus");
			if (sxdsValue == null || sxdsValue == 0) {
				sxdsValue = DcSaleStatus.SALE_UNOPEN.getValue();
			}
			Integer jqsValue = CoreJSONUtils.getInt(object, "jqsStatus");
			if (jqsValue == null || jqsValue == 0) {
				jqsValue = DcSaleStatus.SALE_UNOPEN.getValue();
			}
			Integer bfValue = CoreJSONUtils.getInt(object, "bfStatus");
			if (bfValue == null || bfValue == 0) {
				bfValue = DcSaleStatus.SALE_UNOPEN.getValue();
			}
			Integer bcsfpValue = CoreJSONUtils.getInt(object, "bcsfpStatus");
			if (bcsfpValue == null || bcsfpValue == 0) {
				bcsfpValue = DcSaleStatus.SALE_UNOPEN.getValue();
			}
			item.setSfpStatus(DcSaleStatus.getItem(sfpValue));
			item.setSxdsStatus(DcSaleStatus.getItem(sxdsValue));
			item.setJqsStatus(DcSaleStatus.getItem(jqsValue));
			item.setBfStatus(DcSaleStatus.getItem(bfValue));
			item.setBcsfpStatus(DcSaleStatus.getItem(bcsfpValue));
			items.add(item);
		}
		return items;
	}

}
